#ifndef VIDEO_H
#define VIDEO_H

#include <QObject>
#include <opencv2/opencv.hpp>
#include <QRunnable>
#include <QDebug>
#include <QList>
#include <QMessageBox>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDir>
#include <QSharedMemory>



class Video : public QRunnable
{
public:
    Video(const QList<QString>& fileList, const QString& appDirPath, QList<QPair<QString, QVector<QPair<QString, QJsonArray>>>>& listpair) : m_fileList(fileList), m_appDirPath(appDirPath), m_listpair(listpair)
    {
        m.setKey("key");
        if (!m.create(1))
            if(m.error()==QSharedMemory::AlreadyExists)
                m.attach();

        m1.setKey("key1");
        if (!m1.create(sizeof(int)))
            if(m1.error()==QSharedMemory::AlreadyExists)
                m1.attach();


    };

private:
    bool splitVideoByFixedFrames(const std::string& inputVideoPath, const std::string& outputVideoPrefix, int framesPerSegment);
    void run() override
    {
        // 这里是线程要执行的任务代码
        for (const QString &i : m_fileList) {

            cv::VideoCapture cap(i.toStdString());
            if (!cap.isOpened()) {
                qDebug() << "无法打开视频文件: " << QString::fromStdString(i.toStdString());
            }
            int getTotalFrame = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_COUNT));
            cap.release();
            if (getTotalFrame > 2000) {
                QFileInfo fileInfo(i);
                QString fileName = fileInfo.completeBaseName();
                QString str = m_appDirPath + "/temp/" + fileName + "/";
                QDir().mkpath(str);
                QPair<QString, QVector<QPair<QString, QJsonArray>>> pair;
                QVector<QPair<QString, QJsonArray>> vector;
                if (splitVideoByFixedFrames(i.toStdString(), str.toStdString(), 2000)) {
                    int num = 0;
                    do {
                        QPair<QString, QJsonArray> _pair;
                        _pair.first = str + QString::number(num) + ".mp4";
                        _pair.second = QJsonArray();
                        vector.append(_pair);
                        num++;
                        getTotalFrame -= 2000;
                    } while (getTotalFrame > 0);
                    pair.first = i;
                    pair.second = vector;
                }
                m_listpair.append(pair);
            } else {
                QPair<QString, QVector<QPair<QString, QJsonArray>>> pair;
                QVector<QPair<QString, QJsonArray>> vector;
                QPair<QString, QJsonArray> _pair;
                _pair.first = i;
                _pair.second = QJsonArray();
                vector.append(_pair);
                vector.append(_pair);
                pair.first = i;
                pair.second = vector;
                m_listpair.append(pair);
                if(m1.lock())
                {
                    int data;
                    memcpy(&data,m1.data(),sizeof(int));
                    data++;
                    qDebug()<<"wt2"<<data;
                    memcpy(m1.data(),&data,sizeof(int));
                    m1.unlock();
                }
            }
        }
        if(m.lock())
        {
            bool data = true;
            memcpy(m.data(),&data,1);
            m.unlock();
        }
    }
private:
    QList<QString> m_fileList;
    QString m_appDirPath;
    QList<QPair<QString, QVector<QPair<QString, QJsonArray>>>>& m_listpair;
    QSharedMemory m ;
    QSharedMemory m1 ;
};



#endif // VIDEO_H
